﻿using System.IO;

namespace DataScripter.Scripting.Output
{
    public class MemoryScriptResult : IScriptResult
    {
        private readonly MemoryStream _stream;
        private readonly StreamWriter _writer;

        public MemoryScriptResult()
        {
            _stream = new MemoryStream();
            _writer = new StreamWriter(_stream);
        }

        public string Output { get; set; }

        public string Filename
        {
            get { return null; }
        }

        public StreamWriter Writer
        {
            get { return _writer; }
        }

        public void Close()
        {
            _writer.Flush();
            _stream.Position = 0;
            Output = new StreamReader(_stream).ReadToEnd();

            _writer.Close();
            _writer.Dispose();
        }
    }
}